<?php

/**
 * This is the model class for table "{{location_tags}}".
 *
 * The followings are the available columns in table '{{location_tags}}':
 * @property integer $id
 * @property string $id_page
 * @property string $name
 * @property string $created
 * @property string $modified
 */
class LocationTags extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return LocationTags the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_location_tags';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_page', 'length', 'max'=>50),
			array('name', 'length', 'max'=>100),
			array('created, modified', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, id_page, name, created, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_page' => 'Id Page',
			'name' => 'Name',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('id_page',$this->id_page,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public static function LocationTagsByPageId($id)
	{

//please enter current controller name because yii send multi dim array 
    
	$data=LocationTags::model()->findAll('id_page=:id_page', 
                  array(':id_page'=>(int) $id));
 
    return CHtml::listData($data,'id','name');
   
	}
	
public static function getList($type, $arrParams)
	{
	
		switch($type)
		{

		case "COMMA-SEPARATED-IDS":	
				$arrLocations=explode(",", $arrParams['locationids']);
				$arrLocations=array_filter($arrLocations);
				$condition=null;
				$names=null;
				foreach ($arrLocations as $key=>$value)
				{
					if($condition==null)
					$condition= 'id = '.$value;
					else 
					$condition=$condition.' OR id = '.$value;
				}
				$criteria = new CDbCriteria();
	            $criteria->condition = $condition;
				$result = LocationTags::model()->findAll($criteria);
				if ($result)
				{
					foreach ($result as $value)
					{
						if ($names==null)
						$names=$value->name;
						else 
						$names=$names.", ".$value->name;
					}
				}
		return $names;
		break;
		
		}


	}
	
}